<template>
  <div>


    <div>
      <br>
      流程名称：
      <el-input v-model="processName" placeholder="请输入流程名称" style="width: 150px;" />&nbsp;&nbsp;
      <el-button type="success" @click="fetchData">查询</el-button>
      <br>
      <br>
      <el-button plain @click="dialogVisible = true, resetForm(ruleFormRef)">新增 </el-button>
      <br>
      <br>
      <el-table :data="tableData" class="table">
        <el-table-column type="index" label="序号" width="60" align="center" :index="indexMethod" />
        <el-table-column prop="processName" label="流程名称" style="width: 250px;" />
        <el-table-column prop="formName" label="对应表单" style="width: 250px;" />
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
        :page-sizes="[15, 20, 25, 30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"></el-pagination>

      <!--弹出框新增-->
      <el-dialog v-model="dialogVisible" title="新增表单" width="500">
        <span>
          <el-form ref="ruleFormRef" style="max-width: 600px" :model="RuleForm" :rules="rules" label-width="auto"
            class="demo-ruleForm" status-icon>

            <el-form-item label="流程名称" prop="processName">
              <el-input v-model="RuleForm.processName" />
            </el-form-item>

            <el-form-item label="对应表单" prop="formId">
              <el-select v-model="RuleForm.formId" placeholder="请选择对应表单">
                <el-option v-for="option in FormArray" :key="option.value" :label="option.txt" :value="option.value" />
              </el-select>
            </el-form-item>

            <el-form-item label="流程描述" prop="processDescription">
              <el-input v-model="RuleForm.processDescription" autocomplete="off" />
            </el-form-item>
            <el-form-item label="显示顺序" prop="displaySequence">
              <el-input v-model="RuleForm.displaySequence" autocomplete="off" />
            </el-form-item>

            <el-form-item label="是否有效" prop="whetherEffective">
              <el-select v-model="RuleForm.whetherEffective" placeholder="请选择是否有效">
                <el-option v-for="option in EffectiveArray" :key="option.value" :label="option.txt"
                  :value="option.value" />
              </el-select>
            </el-form-item>

            <el-form-item label="流程提醒" prop="processReminderArr">
              <el-checkbox-group v-model="RuleForm.processReminderArr">
                <el-checkbox value="启用消息提醒" label="启用消息提醒">启用消息提醒</el-checkbox>
                <el-checkbox value="启用邮件提醒" label="启用邮件提醒">启用邮件提醒</el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item label="提交流程后显示的内容" prop="content">
              <el-select v-model="RuleForm.content" placeholder="请选择提交流程后显示的内容">
                <el-option v-for="option in ContentArray" :key="option.value" :label="option.txt"
                  :value="option.value" />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleFormRef)">
                新增
              </el-button>
              <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
          </el-form>
        </span>
      </el-dialog>


    </div>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
//关于弹出框
const dialogVisible = ref(false)
//查询框
const processName = ref('')
//分页
const tableData = ref([])
const pageIndex = ref(1)
const pageSize = ref(15)
const totalCount = ref(0)
// 序号
const indexMethod = (index: number) => {
  return index += (pageSize.value) * (pageIndex.value - 1) + 1;
}
//下拉框
interface Ioption {
  txt: string
  value: number
}

//行业下拉框数组
const FormArray = ref<Ioption[]>([])
//是否有效下拉框数组
const EffectiveArray = ref<Ioption[]>([])
//提交流程后显示的内容下拉框数组
const ContentArray = ref<Ioption[]>([])

//显示
const fetchData = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/ShowProcess', {
      params: {
        PageIndex: pageIndex.value,
        PageSize: pageSize.value,
        processName: processName.value,
      }
    })
    tableData.value = response.data.data
    totalCount.value = response.data.totalCount
  } catch (error) {
    console.error(error)
  }
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val: number) => {
  pageIndex.value = val
  fetchData()
}
//获取表单中的信息
const RuleForm = reactive({
  processName: '',
  formId: '',
  processDescription: '',
  displaySequence: '',
  whetherEffective: '',
  processReminder: '',
  processReminderArr: [],
  content: '',
})
const ruleFormRef = ref<FormInstance>()

//验证
const rules = reactive<FormRules>({
  processName: [
    { required: true, message: '流程名称不能为空', trigger: 'blur' },
  ],
  formId: [
    { required: true, message: '对应表单不能为空', trigger: 'change' },
  ],
  processDescription: [
    { required: true, message: '流程描述不能为空', trigger: 'blur' },
  ],
  displaySequence: [
    { required: true, message: '显示顺序不能为空', trigger: 'blur' },
  ],
  whetherEffective: [
    { required: true, message: '是否有效不能为空', trigger: 'change' },
  ],
  processReminderArr: [
    {
      type: 'array',
      required: true,
      message: '流程提醒不能为空',
      trigger: 'change',
    },
  ],
  content: [
    { required: true, message: '提交流程后显示的内容不能为空', trigger: 'change' },
  ],

})

//绑定对应表单下拉框
const fetchOptions = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/BinfFormName')
    // 将后端返回的选项数据保存到 options 中
    FormArray.value = response.data
  } catch (error) {
    console.error('获取下拉框选项失败:', error)
  }
}
//绑定是否有效下拉框
const BindEffective = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/zhuanEffective')
    // 将后端返回的选项数据保存到 options 中
    EffectiveArray.value = response.data
  } catch (error) {
    console.error('获取下拉框选项失败:', error)
  }
}
//绑定显示的内容下拉框
const BindContent = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/zhuanDisplayContent')
    // 将后端返回的选项数据保存到 options 中
    ContentArray.value = response.data
  } catch (error) {
    console.error('获取下拉框选项失败:', error)
  }
}
//新增功能
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      RuleForm.processReminder = RuleForm.processReminderArr.join(',');
      axios.post('http://47.95.178.186:8000/IncidentSetRout/api/FormManagement/CreateFlow', RuleForm)
        .then(response => {
          if (response.data.code === 200) {
            dialogVisible.value = false
            router.go(0)
            fetchData()
            ElMessage.success(response.data.msg)
          } else {
            // 处理新增失败情况
            ElMessage.error(response.data.msg)
          }
        })

    } else {
      console.log('error submit!', fields)
    }
  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
interface Tree {
  [key: string]: any
}
//绑定对应表单下拉
onMounted(() => {
  fetchOptions()
  BindEffective()
  BindContent()
  setTimeout(() => {
    //显示
    fetchData()
  }, 200);
})
</script>
